home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 November: Tool Chest / Dev.CD Nov 00 TC Disk 1.toast / What's New? / Sample Code / Graphics 2D / DrawLine / DrawLine.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-09-28  |  4.0 KB  |  193 lines  |  [TEXT/CWIE]

  1. /*
  2.     File:        DrawLine.c
  3.  
  4.     Contains:    This application shows how to use the QuickDraw     
  5.                 pattern mode 'srcCopy' to invert the intersection    
  6.                 of the two green lines.    
  7.  
  8.     Written by: GH    
  9.  
  10.     Copyright:    Copyright © 1995-1999 by Apple Computer, Inc., All Rights Reserved.
  11.  
  12.                 You may incorporate this Apple sample source code into your program(s) without
  13.                 restriction. This Apple sample source code has been provided "AS IS" and the
  14.                 responsibility for its operation is yours. You are not permitted to redistribute
  15.                 this Apple sample source code as "Apple sample source code" after having made
  16.                 changes. If you're going to re-distribute the source, we require that you make
  17.                 it clear in the source that the code was descended from Apple sample source
  18.                 code, but that you've made changes.
  19.  
  20.     Change History (most recent first):
  21.                 08/2000        JM                Carbonized, non-Carbon code is commented out
  22.                                             for demonstration purposes.
  23.                 7/9/1999    KG                Updated for Metrowerks Codewarror Pro 2.1
  24.                 
  25.  
  26. */
  27. #include "CarbonPrefix.h"
  28. #include <Controls.h>
  29. #include <Quickdraw.h>
  30. #include <Fonts.h>
  31. #include <Windows.h>
  32. #include <Menus.h>
  33. #include <TextEdit.h>
  34. #include <Dialogs.h>
  35. #include <Sound.h>
  36.  
  37. #define KBaseResID            128
  38. #define kMoveToFront        (WindowPtr)-1L
  39.  
  40.  
  41. /*********************/
  42. /*    Functions         */
  43. /*********************/
  44.  
  45. void    ToolBoxInit( void );
  46. void    WindowInit( void );
  47. void    DrawLine( void );
  48. void    doEventLoop( void );
  49.  
  50.  
  51. /*************** main ***************/
  52.  
  53. void    main( void )
  54.  
  55. {
  56.     ToolBoxInit();
  57.     WindowInit();
  58.     //DrawLine();
  59.     
  60.     //while ( !Button() );
  61.     doEventLoop();
  62.     
  63. }
  64.  
  65. /*************** ToolBoxInit ***************/
  66.  
  67. void    ToolBoxInit( void )
  68.  
  69. {
  70.     // Toolbox initialization taken care of in carbon, the exception
  71.     // being the InitCursor call
  72.     
  73.     //InitGraf(&qd.thePort);          /* init Quickdraw and global variables        */
  74.     //InitFonts();
  75.     //InitWindows();
  76.     //InitMenus();
  77.     //TEInit();
  78.     //InitDialogs( nil );
  79.     InitCursor();
  80.     
  81. }
  82.  
  83. /*************** WindowInit ***************/
  84.  
  85. void    WindowInit( void )
  86.  
  87. {
  88.     WindowPtr    window;
  89.  
  90.     window = GetNewCWindow( KBaseResID, nil, kMoveToFront );
  91.     
  92.     if ( window == nil )
  93.     {
  94.         SysBeep ( 10 );    /* Couldn't load the WIND resource! */
  95.         ExitToShell();
  96.     }
  97.     
  98.     ShowWindow( window );
  99.     SetPortWindowPort( window );
  100. }
  101.  
  102. /*************** Draw two Lines ***************/
  103.  
  104. void    DrawLine( )
  105. {
  106.     Rect        pictureRect;
  107.     WindowPtr    window;    
  108.     GrafPtr        oldPort;
  109.     
  110.     GetPort(&oldPort);
  111.     
  112.     window    =    FrontWindow();
  113.     SetPortWindowPort(window);
  114.     // Can't directly access in carbon
  115.     //pictureRect = window->portRect;
  116.     GetPortBounds(GetWindowPort(window), &pictureRect);
  117.     
  118.     PenNormal() ;
  119.     PenSize(5,5);
  120.  
  121.     ForeColor( blackColor );
  122.         PenMode(srcCopy);
  123.          MoveTo(  50, 47 );
  124.         LineTo( 300,231 );
  125.         
  126.          PenMode(srcXor);
  127.         MoveTo( 400, 47 );
  128.         LineTo( 200,231 ); 
  129.     
  130.     
  131.        ForeColor( greenColor );
  132.         PenMode(addMax);
  133.          MoveTo( 50, 47 );
  134.         LineTo(300,231);
  135.         
  136.         PenMode(addMax);
  137.         MoveTo( 400, 47 );
  138.         LineTo( 200,231 ); 
  139.         
  140.      SetPort(oldPort);
  141.     
  142. }
  143.  
  144. /*************** The Event Loop ***************/
  145. void doEventLoop()
  146. {
  147.     EventRecord anEvent;
  148.     WindowPtr   evtWind;
  149.     short       clickArea;
  150.     Rect        screenRect;
  151.     Point        thePoint;
  152.  
  153.     for (;;)
  154.     {
  155.         if (WaitNextEvent( everyEvent, &anEvent, 0, nil ))
  156.         {
  157.             if (anEvent.what == mouseDown)
  158.             {
  159.                 clickArea = FindWindow( anEvent.where, &evtWind );
  160.                 
  161.                 if (clickArea == inDrag)
  162.                 {
  163.                     GetRegionBounds( GetGrayRgn(), &screenRect );
  164.                     DragWindow( evtWind, anEvent.where, &screenRect );
  165.                 }
  166.                 else if (clickArea == inContent)
  167.                 {
  168.                     if (evtWind != FrontWindow())
  169.                         SelectWindow( evtWind );
  170.                     else
  171.                     {
  172.                         thePoint = anEvent.where;
  173.                         GlobalToLocal( &thePoint );
  174.                         //Handle click in window content here
  175.                     }
  176.                 }
  177.                 else if (clickArea == inGoAway)
  178.                     if (TrackGoAway( evtWind, anEvent.where ))
  179.                         return;
  180.             }
  181.             else if (anEvent.what == updateEvt)
  182.             {
  183.                 evtWind = (WindowPtr)anEvent.message;    
  184.                 SetPortWindowPort( evtWind );
  185.                 
  186.                 BeginUpdate( evtWind );
  187.                 //Call Draw Function here....
  188.                 DrawLine();
  189.                 EndUpdate (evtWind);
  190.             }
  191.         }
  192.     }
  193. }